package org.asiainfo.flowable.service.impl;

import cn.hutool.core.util.ObjectUtil;
import org.asiainfo.common.core.utils.StringUtils;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.flowable.domain.vo.WfCategoryVo;
import org.asiainfo.flowable.domain.bo.WfCategoryBo;
import org.asiainfo.flowable.domain.WfCategory;
import org.asiainfo.flowable.service.WfCategoryService;
import org.springframework.stereotype.Service;
import org.asiainfo.common.core.utils.MapstructUtils;
import lombok.extern.slf4j.Slf4j;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.asiainfo.flowable.mapper.WfCategoryMapper;
import com.mybatisflex.core.paginate.Page;

import java.util.Collection;
import java.util.List;

/**
 * 流程分类表(WfCategory)表服务实现类
 *
 * @author dotor-ww
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class WfCategoryServiceImpl extends ServiceImpl<WfCategoryMapper, WfCategory> implements WfCategoryService {

    /**
     * 构建查询条件
     *
     * @param wfCategoryBo 流程分类表
     * @return 构建结果
     */
    private QueryWrapper builderQueryWrapper(WfCategoryBo wfCategoryBo) {
        QueryWrapper query = query();
        query.like(WfCategory::getCategoryName, wfCategoryBo.getCategoryName(), StringUtils.isNotBlank(wfCategoryBo.getCategoryName()));
        query.eq(wfCategoryBo.getCode(), wfCategoryBo.getCode(), StringUtils.isNotBlank(wfCategoryBo.getCode()));
        return query;
    }

    /**
     * 查询
     *
     * @param wfCategoryBo 筛选条件
     * @return 查询结果
     */
    @Override
    public List<WfCategoryVo> queryList(WfCategoryBo wfCategoryBo) {
        QueryWrapper query = builderQueryWrapper(wfCategoryBo);
        return listAs(query, WfCategoryVo.class);
    }

    /**
     * 通过categoryId查询单条数据
     *
     * @param categoryId 主键
     * @return 实例对象
     */
    @Override
    public WfCategoryVo queryById(Long categoryId) {
        return getOneAs(query().eq(WfCategory::getCategoryId, categoryId), WfCategoryVo.class);
    }

    /**
     * 分页查询
     *
     * @param wfCategoryBo 筛选条件
     * @param pageQuery    分页对象
     * @return 查询结果
     */
    @Override
    public TableDataInfo<WfCategoryVo> queryPageList(WfCategoryBo wfCategoryBo, PageQuery pageQuery) {
        QueryWrapper query = builderQueryWrapper(wfCategoryBo);
        pageQuery.buildOrder(query);
        Page<WfCategoryVo> page = pageAs(pageQuery.buildPage(), query, WfCategoryVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增数据
     *
     * @param wfCategoryBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insertByBo(WfCategoryBo wfCategoryBo) {
        WfCategory wfCategory = MapstructUtils.convert(wfCategoryBo, WfCategory.class);
        return save(wfCategory);
    }

    /**
     * 修改数据
     *
     * @param wfCategoryBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean updateByBo(WfCategoryBo wfCategoryBo) {
        WfCategory wfCategory = MapstructUtils.convert(wfCategoryBo, WfCategory.class);
        return updateById(wfCategory);
    }

    /**
     * 通过主键删除数据
     *
     * @param categoryIds 主键
     * @return 是否成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> categoryIds) {
        return removeByIds(categoryIds);
    }

    /**
     * 校验categoryCode是否唯一
     *
     * @param wfCategoryBo 实例对象
     * @return 校验结果
     */
    @Override
    public Boolean checkCategoryCodeUnique(WfCategoryBo wfCategoryBo) {
        return exists(query()
            .eq(WfCategory::getCode, wfCategoryBo.getCode())
            .ne(WfCategory::getCategoryId, wfCategoryBo.getCategoryId(), ObjectUtil.isNotNull(wfCategoryBo.getCategoryId())));
    }
}
